package com.wendu.oa.api.security;

import com.wendu.oa.common.Result;
import com.wendu.oa.dto.MenuDto;
import com.wendu.oa.dto.RoleDto;
import com.wendu.oa.dto.UserDto;
import com.wendu.oa.model.Role;
import com.wendu.oa.service.security.RoleService;
import com.wendu.oa.service.security.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/security/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PutMapping("/roles/{u_id}")
    public Result assignRoles(@PathVariable String u_id, @RequestBody Integer[] ro_ids){
        userService.setRoles(u_id,ro_ids);
        return Result.OK();
    }

    @GetMapping("/roles")
    public Result allRoles(){
        List<Role> roleList =  userService.getRoleList();
        return Result.OK(roleList);
    }


    @GetMapping("")
    public Result userList(UserDto userDto){
        return Result.OK(userService.getUserPage(userDto));
    }

    @GetMapping("/roles/{u_id}")
    public Result userRoleIds(@PathVariable String u_id){
        try {
            Integer[] roleIds = userService.getUserRoleIds(u_id);
            return Result.OK(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.err(Result.CODE_ERR_SYS,"系统错误！");
        }
    }


}
